package com.dingqb.ktdemo.oop.delegatemode

import kotlin.reflect.KProperty

class DelegaeMode {
}

// 创建接口
interface Base {
	fun print()
}

// 实现此接口的被委托的类
class BaseImpl(val x: Int) : Base {
	override fun print() {
		println(x)
	}
}

// 通过关键字 by 建立委托类
class Derived(b: Base) : Base by b

class Deriverd2(b: Base) : Base by b

fun BaseIm() {
	val b = BaseImpl(10)
	Derived(b).print() // 输出 10
	b.print()
}

fun main(args: Array<String>) {
//	BaseIm();
	exam();
	println(lazyValue);
	println(v);
}

class Stu {
	var v: String = "";
	operator fun setValue(ref: Any?, property: KProperty<*>, value: String) {
		println("$ref,$property.name,value==$value");
		v = value;
	}

	operator fun getValue(ref: Any?, property: KProperty<*>): String {
		return v;
	}
}

class ExampleS {
	var p: String by Stu();
}

fun exam() {
	var e = ExampleS();
	e.p = "123";
	println(e.p);
}

val lazyValue: String by lazy {
	println("computed!")     // 第一次调用输出，第二次调用不执行
	"I am lazy delegate field - - "
}

val v: String = "I am simple field - - ";

